package com.app.qtjk.lsh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.app.common.dataDictionary.DE.DE_C_BRAND_TOTALWEALTH;
import com.app.common.dataDictionary.DE.DE_OPPTIME;
import com.app.common.dataDictionary.DE.DE_QRCODEID;
import com.app.common.dataDictionary.DE.DE_QRCODETYPE;
import com.app.common.dataDictionary.DE.DE_USERID;
import com.app.common.dataDictionary.DE.DE_USERWEALTH;
import com.app.common.dataDictionary.PO.QrcodemaintainDayPO;
import com.app.common.dataDictionary.PO.QrcodemaintainMonthPO;
import com.app.common.dataDictionary.PO.QrcodemaintainWeekPO;
import com.app.common.dataDictionary.PO.TCategoryShopPO;
import com.app.common.dataDictionary.PO.TUserPO;
import com.app.common.utils.CollectionUtil;
import com.app.common.utils.DateFormatUtil;
import com.app.common.utils.FilterUtil;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class QRcodeService {

	private static SwordLogUtils log = SwordLogUtils
			.getLogger(QRcodeService.class);

	public QRcodeService() throws Exception {
		super();
	}

	/**
	 * 检查二维码是否存在
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "QRcodeService_sfHavePassword")
	public IResData userScanQRcode(IReqData req) throws Exception {

		log.debug("开始进入 QRcodeService_sfHavePassword 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		String qrcodeExist = "false";
		String sfHavePassword = "false";
		try {
			SQLParam param = new SQLParam();
			// 获取前台post的数据
			String qrcodeInfo = ReqJsonUtil.getPostParam(req, "qrcodeInfo");
			qrcodeInfo=new String(qrcodeInfo.getBytes("iso-8859-1"),"utf-8");
			param.addParam(qrcodeInfo);
			// 获取某二维码所有相关信息
			Map<String, Object> map = dao.queryMapByKey("QRcodeService_queryByInfo", param);
			if (CollectionUtil.isNotBlankMap(map)) {
				qrcodeExist = "true";
				if (StringUtil.isNotEmpty(StringUtil.parse2String(map.get("qrcodePassword")))) {
					sfHavePassword = "true";
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}	
		res.put("qrcodeExist", qrcodeExist);
		res.put("sfHavePassword", sfHavePassword);
		
		return res;
	}
	
	/**
	 * 检查用户输入的二维码密码是否正确
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "QRcodeService_checkPassword")
	public IResData checkPassword(IReqData req) throws Exception {
		
		log.debug("开始进入 QRcodeService_checkPassword 方法。");
		
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		
		IResData res = new SwordRes();
		String passwordSfRight = "false";
		try {
			SQLParam param = new SQLParam();
			// 获取前台post的数据
			String postPassword = ReqJsonUtil.getPostParam(req, "qrcodePassword");
			String qrcodeInfo = ReqJsonUtil.getPostParam(req, "qrcodeInfo");
			qrcodeInfo=new String(qrcodeInfo.getBytes("iso-8859-1"),"utf-8");
			param.addParam(qrcodeInfo);
			// 获取某二维码所有相关信息
			Map<String, Object> map = dao.queryMapByKey("QRcodeService_queryByInfo", param);
			String qrcodePassword = StringUtil.parse2String(map.get("qrcodePassword"));
			if (postPassword.equals(qrcodePassword)) {
				passwordSfRight = "true";
			}
		}catch (Exception e) {
			e.printStackTrace();
		}	
		
		res.put("passwordSfRight", passwordSfRight);
		return res;
	
	}
	

	/**
	 * 检查二维码是否存在
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "QRcodeService_addWealth")
	public IResData addWealth(IReqData req) throws Exception {
		
		log.debug("开始进入 QRcodeService_addWealth 方法。");
		
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		dao.getTransactionService().setAutoCommit(false);
		
		IResData res = new SwordRes();
		String sfScan = "false";
		String sfEnough = "false";
		String addSuccess = "false";
		int addWealth = 0;
		try {
			SQLParam param = new SQLParam();
			// 获取前台post的数据
			String userId = ReqJsonUtil.getPostParam(req, "userId");
			String qrcodeInfo = ReqJsonUtil.getPostParam(req, "qrcodeInfo");
			qrcodeInfo=new String(qrcodeInfo.getBytes("iso-8859-1"),"utf-8");
			param.addParam(qrcodeInfo);
			// 获取某二维码所有相关信息
			Map<String, Object> map = dao.queryMapByKey("QRcodeService_queryByInfo", param);
			String qrcodeType = StringUtil.parse2String(map.get("qrcodeType"));
			String qrcodeId = StringUtil.parse2String(map.get("qrcodeId"));
			param.clearParams();
			param.addParam(userId);
			param.addParam(qrcodeId);
			if ("day".equals(qrcodeType)) {
				if (CollectionUtil.isNotBlankMap(dao.queryMapByKey("QRcodeService_sfScanDay", param))) {
					sfScan = "true";
				}
			}else if ("week".equals(qrcodeType)) {
				if (CollectionUtil.isNotBlankMap(dao.queryMapByKey("QRcodeService_sfScanWeek", param))) {
					sfScan = "true";
				}
			}else if ("month".equals(qrcodeType)) {
				if (CollectionUtil.isNotBlankMap(dao.queryMapByKey("QRcodeService_sfScanMonth", param))) {
					sfScan = "true";
				}
			}
			if ("false".equals(sfScan)) {
				int qrcodeAddwealth = (Integer) map.get("qrcodeAddwealth");
				addWealth = qrcodeAddwealth;
				String webusername = StringUtil.parse2String(map.get("webusername"));
				// 获取商户的PO
				param.clearParams();
				param.addParam(webusername);
				TCategoryShopPO shopPO = dao.queryPOByKey("GetShopPOByWebusername", param, TCategoryShopPO.class);
				if (shopPO.getCBrandTotalwealth().value >= qrcodeAddwealth) {
					sfEnough = "true";
				}
				if ("true".equals(sfEnough)) {
					// 获取用户的财富值
					param.clearParams();
					param.addParam(userId);
					int userWealth = (Integer) dao.queryColumnByKey("QueryUserWealthByUserid", param, 1);
					//给用户增加财富值
					TUserPO userPO = new TUserPO();
					userPO.setUserid(new DE_USERID(userId));
					userPO.setUserwealth(new DE_USERWEALTH(userWealth+qrcodeAddwealth));
					int i = dao.updatePO(userPO);
					//给商户减财富值
					shopPO.setCBrandTotalwealth(new DE_C_BRAND_TOTALWEALTH(shopPO.getCBrandTotalwealth().value-qrcodeAddwealth));
					int j = dao.updatePO(shopPO);
					int k = 0;
					if ("day".equals(qrcodeType)) {
						//新增用户扫描二维码记录信息
						QrcodemaintainDayPO dayPO = new QrcodemaintainDayPO();
						dayPO.setUserid(new DE_USERID(userId));
						dayPO.setQrcodeid(new DE_QRCODEID(qrcodeId));
						dayPO.setQrcodetype(new DE_QRCODETYPE(qrcodeType));
						dayPO.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
						k = dao.insertPO(dayPO);
					}else if ("week".equals(qrcodeType)) {
						//新增用户扫描二维码记录信息
						QrcodemaintainWeekPO weekPO = new QrcodemaintainWeekPO();
						weekPO.setUserid(new DE_USERID(userId));
						weekPO.setQrcodeid(new DE_QRCODEID(qrcodeId));
						weekPO.setQrcodetype(new DE_QRCODETYPE(qrcodeType));
						weekPO.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
						k = dao.insertPO(weekPO);
					}else if ("month".equals(qrcodeType)) {
						//新增用户扫描二维码记录信息
						QrcodemaintainMonthPO monthPO = new QrcodemaintainMonthPO();
						monthPO.setUserid(new DE_USERID(userId));
						monthPO.setQrcodeid(new DE_QRCODEID(qrcodeId));
						monthPO.setQrcodetype(new DE_QRCODETYPE(qrcodeType));
						monthPO.setOpptime(new DE_OPPTIME(DateFormatUtil.getNowDate()));
						k = dao.insertPO(monthPO);
					}else if ("no".equals(qrcodeType)){
						k=1;
					}
					if (i>0 && j>0 && k>0) {
						dao.commitAll();
						addSuccess = "true";
						param.clearParams();
						param.addParam(qrcodeInfo);
						dao.executeByKey("QRcodeService_updateCs", param);
					}
				}
			}
		}catch (Exception e) {
			dao.rollbackAll();
			e.printStackTrace();
		}	
		
		res.put("sfScan", sfScan);
		res.put("sfEnough", sfEnough);
		res.put("addSuccess", addSuccess);
		res.put("addWealth", addWealth);
		return res;
	}
	
	/**
	 * 给二维码增加一扫描次数
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "QRcodeService_addOneCs")
	public IResData addOneCs(IReqData req) throws Exception{

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		log.debug("开始进入 QRcodeService_addOneCs 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
				hashMap.put("returnStatus", "true");
				String qrcodeInfo = ReqJsonUtil.getPostParam(req, "qrcodeInfo");
				qrcodeInfo=new String(qrcodeInfo.getBytes("iso-8859-1"),"utf-8");
				SQLParam param = new SQLParam();
				param.addParam(qrcodeInfo);
				int j = dao.executeByKey("QRcodeService_updateCs", param);
				if (j > 0) {
					hashMap.put("success", "true");
				}else {
					hashMap.put("success", "false");
				}
			}
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}

}